﻿using System;
using System.Text;
using Cuce.ReportingServices.SSRSWS;
using Cuce.ReportingServices.UnitTests.Mock;
using NUnit.Framework;

namespace Cuce.ReportingServices.UnitTests
{
    /// <summary>
    /// 
    /// </summary>
    [TestFixture]
    public class TestWrapperTest
    {

        private TestWrapper i;
        private const string _validReportServer = "http://melwdscrr01/ReportServer";

        /// <summary>
        /// Tests the properties.
        /// </summary>
        [Test]
        public void TestProperties()
        {

            i = new TestWrapper(new ReportingServicesMock())
                    {
                        ReportServer = _validReportServer,
                        WebProxy = null,
                        Credentials = null
                    };

            Assert.IsNull(i.WebProxy);
            Assert.IsNull(i.Credentials);

        }

        /// <summary>
        /// Tests if URL set render report1.
        /// </summary>
        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void TestIfURLSetRenderReport1()
        {


            i = new TestWrapper(new ReportingServicesMock());

            i.RenderReport(new ReportName("name", "/CRR/"), new ParameterValue[] { }, OutputType.CSV);
            Assert.IsTrue(true);


        }

        /// <summary>
        /// Tests if parameter NULL render report1.
        /// </summary>
        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void TestIfParameterNULLRenderReport1()
        {


            i = new TestWrapper(new ReportingServicesMock()) { ReportServer = _validReportServer };

            i.RenderReport(new ReportName("name", "/CRR/"), null, OutputType.CSV);

            Assert.IsTrue(false);


        }

        /// <summary>
        /// Tests if name blank render report1.
        /// </summary>
        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void TestIfNameBlankRenderReport1()
        {

            i = new TestWrapper(new ReportingServicesMock()) { ReportServer = _validReportServer };

            i.RenderReport(new ReportName("", "/"), new ParameterValue[] { }, OutputType.CSV);
            Assert.IsTrue(false);


        }


        /// <summary>
        /// Tests if RS warnings thrown render report1.
        /// </summary>
        [Test]
        public void TestIfRSWarningsThrownRenderReport1()
        {
            try
            {
                i = new TestWrapper(new ReportingServicesMock()) { ReportServer = _validReportServer };


                var list = new ParameterValue[1];
                list[0] = new ParameterValue { Name = "Warning" };

                i.RenderReport(new ReportName("name", "/CRR/"), list, OutputType.CSV);


                Assert.IsTrue(false);
            }
            catch (ReportException e)
            {

                if (e.Warnings.Length > 0)
                {
                    Assert.IsTrue(e.Warnings[0].Message == "Warning");
                }
                else
                    Assert.IsTrue(false);
            }


        }

        /// <summary>
        /// Tests if RS errorhrown render report1.
        /// </summary>
        [Test]
        [ExpectedException(typeof(ReportException))]
        public void TestIfRSErrorhrownRenderReport1()
        {

            i = new TestWrapper(new ReportingServicesMock()) { ReportServer = _validReportServer };


            var list = new ParameterValue[1];
            list[0] = new ParameterValue { Name = "Error" };

            i.RenderReport(new ReportName("name", "/CRR/"), list, OutputType.CSV);


            Assert.IsTrue(false);



        }


        /// <summary>
        /// Tests if URL set render report2.
        /// </summary>
        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void TestIfURLSetRenderReport2()
        {


            i = new TestWrapper(new ReportingServicesMock());

            i.RenderReport(new ReportName("name", "/CRR/"), new ParameterValue[] { }, OutputType.CSV, null);

            Assert.IsTrue(true);


        }

        /// <summary>
        /// Tests if parameter NULL render report2.
        /// </summary>
        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void TestIfParameterNULLRenderReport2()
        {


            i = new TestWrapper(new ReportingServicesMock()) { ReportServer = _validReportServer };

            i.RenderReport(new ReportName("name", "/CRR/"), null, OutputType.CSV, null);
            Assert.IsTrue(false);


        }

        /// <summary>
        /// Tests if name blank render report2.
        /// </summary>
        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void TestIfNameBlankRenderReport2()
        {


            i = new TestWrapper(new ReportingServicesMock()) { ReportServer = _validReportServer };


            i.RenderReport(new ReportName("", "/"), new ParameterValue[] { }, OutputType.CSV, null);

            Assert.IsTrue(false);


        }


        /// <summary>
        /// Tests if name null render report2.
        /// </summary>
        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void TestIfNameNullRenderReport2()
        {


            i = new TestWrapper(new ReportingServicesMock()) { ReportServer = _validReportServer };

            i.RenderReport(null, new ParameterValue[] { }, OutputType.CSV, null);

            Assert.IsTrue(false);


        }

        /// <summary>
        /// Tests if name null render report2.
        /// </summary>
        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void TestIfNameNull2RenderReport2()
        {


            i = new TestWrapper(new ReportingServicesMock()) { ReportServer = _validReportServer };

            i.RenderReport(null, new ParameterValue[] { }, OutputType.CSV);

            Assert.IsTrue(false);


        }

        /// <summary>
        /// Tests if RS warnings thrown render report2.
        /// </summary>
        [Test]
        public void TestIfRSWarningsThrownRenderReport2()
        {
            try
            {
                i = new TestWrapper(new ReportingServicesMock()) { ReportServer = _validReportServer };


                var list = new ParameterValue[1];
                list[0] = new ParameterValue { Name = "Warning" };

                i.RenderReport(new ReportName("name", "/CRR/"), list, OutputType.CSV, null);


                Assert.IsTrue(false);
            }
            catch (ReportException e)
            {

                if (e.Warnings.Length > 0)
                {
                    Assert.IsTrue(e.Warnings[0].Message == "Warning");
                }
                else
                    Assert.IsTrue(false);
            }


        }

        /// <summary>
        /// Tests if RS errorhrown render report2.
        /// </summary>
        [Test]
        [ExpectedException(typeof(ReportException))]
        public void TestIfRSErrorhrownRenderReport2()
        {

            i = new TestWrapper(new ReportingServicesMock()) { ReportServer = _validReportServer };


            var list = new ParameterValue[1];
            list[0] = new ParameterValue { Name = "Error" };

            i.RenderReport(new ReportName("name", "/CRR/"), list, OutputType.CSV, null);


            Assert.IsTrue(false);



        }

        /// <summary>
        /// Tests the valid data source.
        /// </summary>
        [Test]
        public void TestValidDataSource()
        {

            i = new TestWrapper(new ReportingServicesMock()) { ReportServer = _validReportServer };


            var list = new ParameterValue[1];

            list[0] = new ParameterValue { Name = "Nothing" };

            var ds = new DataSource[1];
            ds[0] = new DataSource();


            byte[] results = i.RenderReport(new ReportName("name", "/CRR/"), list, OutputType.CSV, ds);

            var encoding = new ASCIIEncoding();
            byte[] results2 = encoding.GetBytes(new ReportName("name", "/CRR/").ToString());

            Assert.AreEqual(results2, results);



        }

    }
}
